package pl.edu.pw.mini.java2.Communicator.Client;

import java.io.*;

import javax.swing.JOptionPane;

public class ReaderWorker extends Thread {

	private DataInputStream in;
	private Client client;
	private int lastState = -1;

	public ReaderWorker(DataInputStream in, Client client) {
		this.in = in;
		this.client = client;
	}

	public void close() {
		try {
			in.close();
		} catch (IOException ignored) {
		}
		in = null;
		client = null;
	}

	@Override
	public void run() {
		while (true) {
			try {
				handleMessage(Message.read(in));
			} catch (EOFException ex) {
				System.out.println("Client: Koniec danych!");
				JOptionPane.showMessageDialog(null, "Lost server connection.");
				System.exit(1);
				break;
			} catch (IOException ex) {
				System.out.println("Client: Error IO");
				JOptionPane.showMessageDialog(null, "Lost server connection.");
				System.exit(1);
				break;
			}
		}
		// Finish job
	}

	private void handleMessage(Message m) {
		switch (m.getType()) {
		case Message.OK:
			setLastState(Message.OK);
			break;
		case Message.ERROR:
			setLastState(Message.ERROR);
			break;
		case Message.GET_AVAILABLE_CONTACT:
			handleGetAvailableContactRequest(m);
			break;
		case Message.GET_LEAVING_CONTACT:
			handleGetLeavingContactRequest(m);
			break;
		case Message.CHAT_REJECT:
			handleCharRejectRequest();
			break;
		case Message.CHAT_INVITATION:
			handleChatInvitationRequest(m);
			break;
		case Message.TEXT_MESSAGE:
			handleTextMessageRequest(m);
			break;
		case Message.VOICE_MESSAGE:
			handleVoiceMessageRequest(m);
			break;
		default:
			System.out.println("Unknown message type :" + m.getType());
		}
	}

	private void handleVoiceMessageRequest(Message m) {
		SoundPlayer.getInstance().playMessage(m);
	}

	private void handleTextMessageRequest(Message m) {
		if (client.getChatListener() != null) {
			client.getChatListener()
					.readTextMessage(new String(m.getMessage()));
		}
	}

	private void handleChatInvitationRequest(Message m) {
		ClientListener clientListener;
		clientListener = client.getClientListener();
		if (clientListener != null) {
			clientListener.chatInvitation(new String(m.getMessage()));
		}
	}

	private void handleCharRejectRequest() {
		ChatListener chatListener;
		chatListener = client.getChatListener();
		if (chatListener != null) {
			chatListener.chatReject();
		}
	}

	private void handleGetLeavingContactRequest(Message m) {
		ClientListener clientListener;
		clientListener = client.getClientListener();
		if (clientListener != null) {
			clientListener.getLeavingContact(new String(m.getMessage()));
		}
	}

	private void handleGetAvailableContactRequest(Message m) {
		ClientListener clientListener;
		clientListener = client.getClientListener();
		if (clientListener != null) {
			clientListener.getAvailableContacts(new String(m.getMessage()));
		}
	}

	public synchronized void setLastState(int lastState) {
		this.lastState = lastState;
	}

	public synchronized int getLastState() {
		return lastState;
	}
}